<?php
/**
* Entidad entrada de diario.
*/
class Entry {
	
	CONST DB_TENTRY = "entry";
	CONST DB_TENTRY_ID = "identry";
	CONST DB_TENTRY_AUTOR = "autor";
	CONST DB_TENTRY_CREATED = "created";
	CONST DB_TENTRY_TEXT = "text";
	CONST DB_TENTRY_ACTIVE = "active";
	CONST DB_TENTRY_LUPDATE = "lastupdate";
	CONST DB_TENTRY_FEELING = "feeling";
	CONST DB_TENTRY_ENDED = "ended";
	
	CONST DB_TEIMAGE = "entry_has_image";
	
	CONST DB_TEGPLACE = "entry_in_group_in_place";
	
	CONST DB_TCOMMENTENTRY = "commententry";
	CONST DB_TCOMMENTENTRY_ID = "idcommententry";
	CONST DB_TCOMMENTENTRY_AUTOR = "autor";
	CONST DB_TCOMMENTENTRY_COMMENT = "comment";
	
	CONST DB_IMAGE = "image";
	CONST DB_IMAGE_ID = "idimage";
	CONST DB_IMAGE_URL = "url";
	
	
	/**
	 * Identificador de la entrada
	 * */
	private $identry;
	/**
	 * Identificador del autor de la entrada
	 * */
	private $autor;
	
	/**
	 * Texto asociado a la entrada
	 * */
	private $text;
	
	/**
	 * Imagenes
	 * */
	private $images;
	
	private $comments;
	
	private $idgroup, $idplace;
	
	private $feeling;
	
	private $lastupdate, $created;
	
	private $ended;
	
	public function putLastUpdate($lu){
		$this->lastUpdate = $lu;
	}
	
	public function putId($id){
		$this->identry = $id;
		
	}
	
	public function putIdPlace($idPlace){
		$this->idplace = $idPlace;
	}
	
	public function putIdGroup($idGroup){
		$this->idgroup = $idGroup;		
	}
	
	public function putAutor($autor){
		$this->autor = $autor;		
	}
	
	public function putText($text){
		$this->text = $text;		
	}
	
	public function putFeeling($feeling){
		$this->feeling = $feeling;		
	}
	
	public function __construct(){
		$this->images = array();
		$this->comments = array();
	}
	
	public function loadFromDB($idEntry){
		$q = "SELECT * FROM ".self::DB_TENTRY." WHERE ".self::DB_TENTRY_ACTIVE." = 1 AND ".self::DB_TENTRY_ID." = ".$idEntry;
		$res = sendsql($q);
		
		if(empty($res)){ return false;
		}else{
			$this->identry = $res[0]['identry'];
			$this->autor = $res[0]['autor'];
			$this->text = $res[0]['text'];
			$this->ended = $res[0]['ended'];
			$this->idgroup = $res[0]['idgroup'];
			$this->idplace = $res[0]['idplace'];
			$this->created = $res[0]['created'];
			$this->feeling = $res[0]['feeling'];
			$this->lastupdate = $res[0]['lastupdate'];
			
			$this->loadComments();
			$this->loadImages();
			
			return true;
		}
	}
	
	/**
	 * Guarda en la base de datos la información del comentario.
	 * */
	public function saveToDB(){
		$this->created = $this->lastupdate = time();
		$q = "INSERT INTO `".self::DB_TENTRY."` (".self::DB_TENTRY_AUTOR .",".self::DB_TENTRY_TEXT.", ".Group::DB_TGROUP_ID.",".Group::DB_COMMENT_IDPLACE.",".self::DB_TENTRY_ACTIVE.",".self::DB_TENTRY_LUPDATE.",".self::DB_TENTRY_CREATED.",".self::DB_TENTRY_FEELING.") VALUES(".$this->autor.",'".formsql(to_utf8($this->text))."',".$this->idgroup.",".$this->idplace.",1,".$this->lastupdate.",".$this->created.",".$this->feeling.")";
		$res = sendsql($q, true);
		
		$this->identry = $res['id'];
	}

	public function loadImages(){
		$q = "SELECT * FROM ".self::DB_IMAGE." WHERE ".self::DB_IMAGE_ID." IN (SELECT ".self::DB_IMAGE_ID." FROM ".self::DB_TEIMAGE." WHERE ".self::DB_TENTRY_ID." = ".$this->identry.")";
		$res = sendsql($q);
		
		$this->images = $res;		
	}
	
	public function addFeeling2Entry($feeling){
		$this->feeling = $feeling;
		$q = "UPDATE ".Entry::DB_TENTRY." SET ".Entry::DB_TENTRY_FEELING." = ".$feeling." WHERE ".Entry::DB_TENTRY_ID." = ".$this->identry;
		sendsql($q);
	}
	
	public function addText2Entry($text){
		$this->text = $text;
		$q = "UPDATE ".Entry::DB_TENTRY." SET ".Entry::DB_TENTRY_TEXT." = '".formsql(to_utf8($text))."' WHERE ".Entry::DB_TENTRY_ID." = ".$this->identry;
		sendsql($q);
	}
	
	public function addComment2Entry($comment, $iduser){
		$q = "INSERT INTO ".self::DB_TCOMMENTENTRY." (".self::DB_TENTRY_ID.", ".self::DB_TCOMMENTENTRY_AUTOR.",".self::DB_TCOMMENTENTRY_COMMENT.",".self::DB_TENTRY_CREATED.") VALUES(".$this->identry.",".$iduser.",'".formsql(to_utf8($comment))."',".time().")";	
		sendsql($q);
		$this->loadComments();
	}
	
	public function isAutorEntry($idUser){
		if($this->autor == $idUser) return true;
		else return false;		
	}
	
	public function loadComments(){
		$q = "SELECT * FROM ".self::DB_TCOMMENTENTRY." WHERE active = 1 AND ".self::DB_TENTRY_ID." = ".$this->identry;
		$res = sendsql($q);
			
		$this->comments = $res;		
	}
	
	public function finish(){
		$this->ended = 1;
		$q = "UPDATE ".Entry::DB_TENTRY." SET ".Entry::DB_TENTRY_ENDED." = 1 WHERE ".Entry::DB_TENTRY_ID." = ".$this->identry;
		sendsql($q);		
	}
	
	public function addNewImage2Entry($filename){
		$created = time();
		$q = "INSERT INTO ".self::DB_IMAGE." (".self::DB_IMAGE_URL.",".self::DB_TENTRY_CREATED.") VALUES('".to_utf8($filename)."',".$created.")";
		$res = sendsql($q, true);
		
		$idImage =  $res['id'];
		
		$i = count($this->images);
		$this->images[$i][self::DB_IMAGE_ID] = $idImage;
		$this->images[$i][self::DB_IMAGE_URL] = $filename;
		$this->images[$i][self::DB_TENTRY_CREATED] = $created;
		$this->images[$i][self::DB_TENTRY_ACTIVE] = 1;
		
		$q = "INSERT INTO ".self::DB_TEIMAGE." (".self::DB_TENTRY_ID.",".self::DB_IMAGE_ID.") VALUES(".$this->identry.",".$idImage.")";
		sendsql($q, true);		
	}
	
	public function tojson(){
		$result = array(
			idOnline => $this->identry,
			idGroup => $this->idgroup,
			idAutor => $this->autor,
			idPlaceRelated => $this->idplace,
			lastUpdate => $this->lastupdate,
			text => $this->text,
			feeling => $this->feeling,
			ended => $this->ended
			//created => $this->created
		);
		
		$result['comments'] = $this->comments;		
		$result['images'] = $this->images;
		
		return $result;
	}
	
}
	
?>